package com.seele.tigerwallet.ui.activtiy;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.seele.tigerwallet.BuildConfig;
import com.seele.tigerwallet.R;
import com.seele.tigerwallet.api.APIException;
import com.seele.tigerwallet.api.APIModule;
import com.seele.tigerwallet.api.APIObjectCallBack;
import com.seele.tigerwallet.model.UserInfo;
import com.seele.tigerwallet.model.VersionModel;
import com.seele.tigerwallet.model.WebModel;
import com.seele.tigerwallet.ui.base.BaseActivity;
import com.seele.tigerwallet.ui.base.TitleBaseActivity;
import com.seele.tigerwallet.ui.fragment.MyFragment;
import com.seele.tigerwallet.ui.popupwindow.UpdatePopupWindow;
import com.seele.tigerwallet.ui.utils.StatusBarUtils;
import com.seele.tigerwallet.utils.Utils;

import retrofit2.Call;

public class AboutActivity extends TitleBaseActivity implements View.OnClickListener {


    private TextView mCurrentVersion;
    private LinearLayout mProtocol;
    private LinearLayout mVersionLog;
    private LinearLayout mCheckUpdate;
    private TextView mNewVersion;
    private ImageView mNewVersionTip;
    private VersionModel mVersionModel;


    @Override
    public int getContainerViewId() {
        return R.layout.activity_about;
    }

    @Override
    protected void initView() {
        setTitle("关于我们");
        mCurrentVersion = (TextView) findViewById(R.id.tv_current_version);
        mProtocol = (LinearLayout) findViewById(R.id.layout_protocol);
        mVersionLog = (LinearLayout) findViewById(R.id.layout_version_log);
        mCheckUpdate = (LinearLayout) findViewById(R.id.layout_version_update);
        mNewVersion = (TextView) findViewById(R.id.tv_version);
        mNewVersionTip = (ImageView) findViewById(R.id.iv_new_version);
        mCurrentVersion.setText(getString(R.string.current_version)+ BuildConfig.VERSION_NAME);
        mProtocol.setOnClickListener(this);
        mVersionLog.setOnClickListener(this);
        mCheckUpdate.setOnClickListener(this);
        checkVersion();

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_protocol: //用户协议
                startWebActivity("instructions", getString(R.string.protocol));
                break;
            case R.id.layout_version_log:
                startWebActivity("versionlog", getString(R.string.version_log));
                break;
            case R.id.layout_version_update:
                checkUpdate();
                break;
            default:
                break;
        }
    }


    private void startWebActivity(String function, String title) {
        WebModel webModel = new WebModel();
        webModel.title = title;
        webModel.isDisplay = false;
        String host = UserInfo.get().loginhost;
        if (!TextUtils.isEmpty(host)){
            String url="http://"+host+"/wallet/v1/desc/%s/cn/android";
            url = String.format(url, function);
            webModel.url=url;
        }
        webModel.isDisplay = true;
        Bundle bundle = new Bundle();
        bundle.putSerializable(WebViewActivtiy.class.getSimpleName(), webModel);
        startActivity(WebViewActivtiy.class, bundle);
    }

    private void checkVersion() {
        String currentVersion = BuildConfig.VERSION_NAME;
        APIModule.
                getApi()
                .getversion(currentVersion)
                .enqueue(new APIObjectCallBack<VersionModel>() {
                    @Override
                    protected void onResponse(VersionModel response) {
                        mVersionModel = response;
                        int ret = Utils.versionCompare(BuildConfig.VERSION_NAME, mVersionModel.ver);
                        if (ret == 2) {
                            mNewVersion.setVisibility(View.VISIBLE);
                            mNewVersion.setText(String.format("有新版本 v%s", mVersionModel.ver));
                            mNewVersionTip.setVisibility(View.VISIBLE);
                        } else {
                            mNewVersion.setVisibility(View.GONE);
                            mNewVersionTip.setVisibility(View.GONE);
                        }
                        mVersionModel.info = new String(Base64.decode(response.info.getBytes(), Base64.DEFAULT));
                    }

                    @Override
                    protected void onResponseError(APIException e) {
                    }

                    @Override
                    protected void onFailureError(Call call, Throwable t) {
                    }
                });
    }

    private void checkUpdate() {
        if (mVersionModel == null || mNewVersion.getVisibility() == View.GONE) {
            Toast.makeText(this, R.string.version_new, Toast.LENGTH_SHORT).show();
            return;
        }
        final UpdatePopupWindow updatePopupWindow = new UpdatePopupWindow(this);
        updatePopupWindow.setTitle(String.format("检测到新版本 v%s", mVersionModel.ver));
        updatePopupWindow.setContent(mVersionModel.info);
        updatePopupWindow.setmOnClickListener(new UpdatePopupWindow.OnClickListener() {
            @Override
            public void onCancel() {
                updatePopupWindow.dismiss();
            }

            @Override
            public void onUpdate() {
                Utils.downLoadAPK(AboutActivity.this, mVersionModel.downloadurl, mVersionModel.ver);
                updatePopupWindow.dismiss();
                Toast.makeText(AboutActivity.this, "后台下载中.", Toast.LENGTH_LONG).show();
            }
        });
        updatePopupWindow.showAtLocation(this);
    }

}

